<?php

namespace App\Http\Controllers;

//查看背包
use App\Common\Sql;
use App\Model\User\UserDiscountInfo;
use Illuminate\Support\Facades\DB;

class ViewBag extends Base
{

    public function logic()
    {
        if (!isset($this->param['uid'])) {
            return $this->returnError(1, "please input uid");
        }
        $uid  = $this->param['uid'];
        $data = array();
        $row  = Sql::select('t_user_bag_info', [['uid', $uid], ['num', '>', 0]]);
        foreach ($row as $info) {
            $item = array();
            //获取物品的信息
            $item_info = $this->get_item_info((int)$info['item_id']);
            if ($item_info == false) {
                continue;
            } else {
                $type = (int)$item_info['type'];
                if ((int)$item_info['icon_id'] == 0) {
                    continue;
                }
                $item['id']   = (int)$info['item_id'];
                $item['type'] = $type;
                $item['name'] = $item_info['name'];
                $item['num']  = (int)$info['num'];
                if ($type == 2 || $type == 3) {
                    $item['reward'] = $item_info['reward'];
                } else if ($type == 4) {
                    return $this->returnError(2, "your bag is error");
                }
            }
            $data['item_list'][] = $item;
        }


//        $row = Sql::select('t_user_discount_info', [['uid', $uid], ['status', 1]]);
//        foreach ($row as $info) {
//            $item = array();
//            //获取物品的信息
////            $item_info = $this->get_item_info((int)$info['item_id']);
//            if ($item_info == false) {
//                continue;
//            } else {
//                $data['discount_list'] = UserDiscountInfo::where('uid',$uid)->where('status',1)->get();
//            }
//            $data['discount_list'][] = $item;
//        }

        $data['discount_list'] = UserDiscountInfo::where('uid',$uid)->where('expire_time','>',date('Y-m-d H:i:s'))->get(['id','match_id','code','reward_type']);

//        $match_id              = DB::table('t_discount_info')->where('uid', $uid)->pluck('match_id')->toArray();
//        $data['discount_list'] = UserDiscountInfo::where('uid', $uid)->whereIn('match_id', function ($q) use ($match_id) {
//            $q->from('t_quickly_grab_info')->whereIn('id', $match_id)->where('expire_time', '>', date('Y-m-d H:i:s'))->select(['id']);
//        })
//            ->get(['id', 'match_id', 'code', 'reward_type']);
        $data['uid']           = $uid;
        return $this->returnData($data);
    }

}